import os
from typing import Optional


class Settings:
    """基础配置管理类"""
    
    def __init__(self):
        # ADB路径配置
        self.adb_path = self._get_adb_path()
        # 命令执行超时时间（秒）
        self.command_timeout = 30
        # 设备连接超时时间（秒）
        self.device_timeout = 10
    
    def _get_adb_path(self) -> str:
        """获取ADB路径"""
        # 优先使用环境变量中的ADB路径
        env_adb = os.environ.get('ADB_PATH')
        if env_adb and os.path.exists(env_adb):
            return env_adb
        
        # 尝试常见的ADB路径
        common_paths = [
            'adb',
            '/usr/local/bin/adb',
            '/usr/bin/adb',
            'C:\\Program Files\\Android\\platform-tools\\adb.exe',
            'C:\\Users\\%USERNAME%\\AppData\\Local\\Android\\Sdk\\platform-tools\\adb.exe'
        ]
        
        for path in common_paths:
            if os.path.exists(path) or self._is_command_available(path):
                return path
        
        # 默认返回adb命令
        return 'adb'
    
    def _is_command_available(self, command: str) -> bool:
        """检查命令是否可用"""
        try:
            import subprocess
            result = subprocess.run([command, 'version'], 
                                  capture_output=True, 
                                  timeout=5)
            return result.returncode == 0
        except Exception:
            return False
    
    def get_adb_path(self) -> str:
        """获取ADB路径"""
        return self.adb_path
    
    def get_command_timeout(self) -> int:
        """获取命令超时时间"""
        return self.command_timeout
    
    def get_device_timeout(self) -> int:
        """获取设备连接超时时间"""
        return self.device_timeout


# 全局配置实例pyt
settings = Settings()
